﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using BeeCoders.Tools.ControlCanvas.Adorners;

namespace BeeCoders.Tools.ControlCanvas
{
    public class DesignerItem : ContentControl
    {
        #region IsSelected
        public bool IsSelected
        {
            get { return (bool)GetValue(IsSelectedProperty); }
            set { SetValue(IsSelectedProperty, value); }
        }

        public static readonly DependencyProperty IsSelectedProperty = DependencyProperty.Register("IsSelected", typeof(bool),
                                      typeof(DesignerItem),
                                      new FrameworkPropertyMetadata(false));
        #endregion

        #region Control linked to this item

        private ControlViewModel control;

        public ControlViewModel Control
        {
            get { return control; }
            set
            {
                if (value == control) return;
                control = value;
            }
        }

        #endregion

        public DesignerItem()
        {
            
        }


        public void DumpDebug()
        {
            Console.WriteLine(@"Parameter: {0}", Control);
            Console.WriteLine(@"IsSelected: {0}", IsSelected);


        }

        public static readonly DependencyProperty MoveThumbTemplateProperty =
            DependencyProperty.RegisterAttached("MoveThumbTemplate", typeof(ControlTemplate), typeof(DesignerItem));

        public static ControlTemplate GetMoveThumbTemplate(UIElement element)
        {
            return (ControlTemplate)element.GetValue(MoveThumbTemplateProperty);
        }

        public static void SetMoveThumbTemplate(UIElement element, ControlTemplate value)
        {
            element.SetValue(MoveThumbTemplateProperty, value);
        }

        static DesignerItem()
        {
            DefaultStyleKeyProperty.OverrideMetadata(typeof(DesignerItem), new FrameworkPropertyMetadata(typeof(DesignerItem)));
        }

        public DesignerItem(ControlViewModel reportParameter)
        {
            Control = reportParameter;
            Loaded += DesignerItemLoaded;
        }

        public DesignerItem(ControlViewModel reportParameter, string labelText)
        {
            Control = reportParameter;
            Loaded += DesignerItemLoaded;
            // MouseDoubleClick+=new MouseButtonEventHandler();
        }


        //protected override void OnMouseDoubleClick(MouseButtonEventArgs e)
        //{
        //    base.OnMouseDoubleClick(e);

        //    var reportParameterViewModel = new ReportParameterViewModelDemo();

        //    bool? result = DialogService.ShowDialog<InputControlPropertyView>(this, reportParameterViewModel);

        //    if ((bool)result)
        //    {
        //    }
        //}


        protected override void OnPreviewMouseDown(MouseButtonEventArgs e)
        {
            base.OnPreviewMouseDown(e);

            var designer = VisualTreeHelper.GetParent(this) as DesignerCanvas;

            if (designer != null)
            {
                if (!designer.IsInDesignMode)
                {
                    e.Handled = false;
                }
                else
                {


                    if ((Keyboard.Modifiers & (ModifierKeys.Shift | ModifierKeys.Control)) != ModifierKeys.None)
                    {
                        IsSelected = !IsSelected;
                    }
                    else
                    {
                        if (!IsSelected)
                        {
                            designer.DeselectAll();
                            IsSelected = true;

                            // if the designer is in LinkedControlSelection, then automatically select also the linked controls
                            if (designer.IsLinkedControlSelection)
                            {
                                if (Control != null)
                                {
                                    if (Control.LinkedControl != null)
                                    {
                                        Control.LinkedControl.Node.IsSelected = true;
                                    }
                                }
                            }
                        }
                    }
                }
            }

            e.Handled = false;
        }

        private void DesignerItemLoaded(object sender, RoutedEventArgs e)
        {
            if (Template != null)
            {
                var contentPresenter = Template.FindName("PART_ContentPresenter", this) as ContentPresenter;

                var thumb = Template.FindName("PART_MoveThumb", this) as MoveThumb;

                if (contentPresenter != null && thumb != null)
                {

                    var contentVisual = VisualTreeHelper.GetChild(contentPresenter, 0) as UIElement;

                    if (contentVisual != null)
                    {
                        var template = GetMoveThumbTemplate(contentVisual);

                        if (template != null)
                        {
                            thumb.Template = template;
                        }
                    }
                }
            }
        }
    }
}
